package com.fileshare.web.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    public static final String EMPTY_STRING = "";
    protected Integer code;
    protected String msg;
    protected T data;

    public static DataBuilder success() {
        return success(HttpStatus.OK.value(), "success");
    }

    public static DataBuilder failed() {
        return failed(HttpStatus.INTERNAL_SERVER_ERROR.value(), "error");
    }

    public static DataBuilder success(int code) {
        return new DefaultBuilder(code, EMPTY_STRING);
    }

    public static DataBuilder success(int code, String msg) {
        return new DefaultBuilder(code, msg);
    }

    public static DataBuilder failed(int code) {
        return new DefaultBuilder(code, EMPTY_STRING);
    }

    public static DataBuilder failed(int code, String msg) {
        return new DefaultBuilder(code, msg);
    }

    public interface DataBuilder {
        <DATA> Result<DATA> with(@Nullable DATA data);

        <DATA> Result<DATA> with();
    }

    private static class DefaultBuilder implements DataBuilder {

        private final Integer code;
        private final String msg;

        public DefaultBuilder(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        @Override
        public <DATA> Result<DATA> with(@Nullable DATA data) {
            return new Result<>(code, msg, data);
        }

        @Override
        public <DATA> Result<DATA> with() {
            return new Result<>(code, msg, null);
        }
    }
}
